package com.kk.xhr.admin.controller;

import com.kk.xhr.admin.biz.IUserBizService;
import com.kk.xhr.admin.controller.request.UserAddRequest;
import com.kk.xhr.admin.controller.request.UserModifyRequest;
import com.kk.xhr.admin.controller.request.UserQueryRequest;
import com.kk.xhr.admin.controller.vo.UserInfoVO;
import com.kk.xhr.admin.controller.vo.UserVO;
import com.kk.xhr.common.pojo.UserStatusEnum;
import com.kk.xhr.web.util.ShiroUtil;
import com.kk.xhr.web.vo.ApiResult;
import com.kk.xhr.web.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

/**
 * 用户控制器
 *
 * @author kkmystery
 * @version 1.0 2021/3/11
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(value = "用户管理", tags = "用户管理")
@RequestMapping("user")
public class UserController {
    private final Executor adminControllerBizExecutor;
    private final IUserBizService userBizService;

    @ApiOperation(value = "获取用户列表信息")
    @RequiresRoles(value = {"admin"})
    @GetMapping
    public CompletionStage<ApiResult<PageVO<UserVO>>> queryPage(@Valid UserQueryRequest request) {
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(userBizService.queryPage(request)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "获取用户列表")
    @GetMapping("/list")
    public CompletionStage<ApiResult<List<UserInfoVO>>> queryList() {
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(userBizService.query()), adminControllerBizExecutor);
    }

    @ApiOperation(value = "添加用户信息")
    @RequiresRoles(value = {"admin"})
    @PostMapping
    public CompletionStage<ApiResult<Void>> add(@RequestBody @Valid UserAddRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            userBizService.add(request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改用户信息")
    @RequiresRoles(value = {"admin"})
    @PutMapping("/{id:.+}")
    public CompletionStage<ApiResult<Void>> modify(@PathVariable("id") Long id, @RequestBody @Valid UserModifyRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            userBizService.modify(id, request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改用户状态")
    @RequiresRoles(value = {"admin"})
    @PutMapping("/status/{id:.+}")
    public CompletionStage<ApiResult<Void>> modifyStatus(@PathVariable("id") Long id, UserStatusEnum status) {
        return CompletableFuture.supplyAsync(() -> {
            userBizService.modifyStatus(id, status);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改个人信息")
    @PutMapping("/mine")
    public CompletionStage<ApiResult<Void>> modifyMine(@RequestBody @Valid UserModifyRequest request) {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() -> {
            userBizService.modifyMine(userId, request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改头像")
    @PutMapping("/avatar/{fileId}")
    public CompletionStage<ApiResult<String>> modifyAvatar(@PathVariable("fileId") String fileId) {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() -> {
            return ApiResult.success(userBizService.modifyAvatar(userId, fileId));
        }, adminControllerBizExecutor);
    }
}


